<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveSupport::Rescuable::ClassMethods</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveSupport::Rescuable::ClassMethods</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/activesupport/lib/active_support/rescuable_rb.html">
                vendor/rails/activesupport/lib/active_support/rescuable.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000269">rescue_from</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000269" class="method-detail">
        <a name="M000269"></a>

        <div class="method-heading">
          <a href="#M000269" class="method-signature">
          <span class="method-name">rescue_from</span><span class="method-args">(*klasses, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Rescue exceptions raised in controller actions.
</p>
<p>
<tt><a href="ClassMethods.html#M000269">rescue_from</a></tt> receives a
series of exception classes or class names, and a trailing <tt>:with</tt>
option with the name of a method or a Proc object to be called to handle
them. Alternatively a block can be given.
</p>
<p>
Handlers that take one argument will be called with the exception, so that
the exception can be inspected when dealing with it.
</p>
<p>
Handlers are inherited. They are searched from right to left, from bottom
to top, and up the hierarchy. The handler of the first class for which
<tt>exception.is_a?(klass)</tt> holds true is the one invoked, if any.
</p>
<pre>
  class ApplicationController &lt; ActionController::Base
    rescue_from User::NotAuthorized, :with =&gt; :deny_access # self defined exception
    rescue_from ActiveRecord::RecordInvalid, :with =&gt; :show_errors

    rescue_from 'MyAppError::Base' do |exception|
      render :xml =&gt; exception, :status =&gt; 500
    end

    protected
      def deny_access
        ...
      end

      def show_errors(exception)
        exception.record.new_record? ? ...
      end
  end
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000269-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000269-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/rescuable.rb, line 44</span>
44:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">rescue_from</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">klasses</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
45:         <span class="ruby-identifier">options</span> = <span class="ruby-identifier">klasses</span>.<span class="ruby-identifier">extract_options!</span>
46: 
47:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:with</span>)
48:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
49:             <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:with</span>] = <span class="ruby-identifier">block</span>
50:           <span class="ruby-keyword kw">else</span>
51:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">&quot;Need a handler. Supply an options hash that has a :with key as the last argument.&quot;</span>
52:           <span class="ruby-keyword kw">end</span>
53:         <span class="ruby-keyword kw">end</span>
54: 
55:         <span class="ruby-identifier">klasses</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">klass</span><span class="ruby-operator">|</span>
56:           <span class="ruby-identifier">key</span> = <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Class</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">klass</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-constant">Exception</span>
57:             <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">name</span>
58:           <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>)
59:             <span class="ruby-identifier">klass</span>
60:           <span class="ruby-keyword kw">else</span>
61:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">&quot;#{klass} is neither an Exception nor a String&quot;</span>
62:           <span class="ruby-keyword kw">end</span>
63: 
64:           <span class="ruby-comment cmt"># put the new handler at the end because the list is read in reverse</span>
65:           <span class="ruby-identifier">rescue_handlers</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-identifier">key</span>, <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:with</span>]]
66:         <span class="ruby-keyword kw">end</span>
67:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>